package com.sitech.openapi.controller;

import com.sitech.openapi.componet.SetHttpHeaderCmpt;
import com.sitech.openapi.service.CustOrderLogisticsService;
import com.sitech.openapi.service.QryWorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangsq on 2018/12/8.
 * 关于随选宽带甩单集约销售支撑工作的通知需求
 * 查询施工工单
 */
@RestController
@Slf4j
@Api(tags = "宽带随选查询施工工单", description = "宽带随选查询施工工单控制层")
public class QryWorkOrderController {
    @Autowired
    private SetHttpHeaderCmpt setHttpHeaderCmpt;
    @Autowired
    QryWorkOrderService qryWorkOrderService;

    @GetMapping("/workOrder")
    @ApiOperation(value = "查询施工工单", notes = "关于随选宽带甩单集约销售支撑工作的通知需求,查询施工工单")
    public ResponseEntity<List> qryWorkOrderBack(@RequestParam String commonRegionId, @RequestParam String extCustOrderId,
                                                 @RequestHeader HttpHeaders headers) throws Exception {
        log.info("入参<><><><>custOrderId=" + extCustOrderId + "<><><><>=commonRegionId" + commonRegionId);
        Map inParam = new HashMap();
        inParam.put("extCustOrderId", extCustOrderId);
        inParam.put("commonRegionId", commonRegionId);
        List list = qryWorkOrderService.qryWorkOrderInfo(inParam);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<List> res = new ResponseEntity<List>(list, httpHeaders, HttpStatus.OK);
        return res;
    }


    @ExceptionHandler
    public ResponseEntity<Map<String, String>> qryWorkOrderError(Exception e, NativeWebRequest request) {
        Map<String, String> errors = new HashMap<>();
        errors.put("code", HttpStatus.BAD_REQUEST.toString());
        errors.put("reason", e.getMessage());
        errors.put("message", e.toString());
        errors.put("referenceError", "");

        HttpHeaders httpHeaders = setHttpHeaderCmpt.setErrorHttpHeaders(request);
        ResponseEntity<Map<String, String>> re = new ResponseEntity<>(errors, httpHeaders, HttpStatus.BAD_REQUEST);
        return re;
    }
}
